import Vue from "vue";
var jweixin = require('./jssdk.js');
import app from './api.js'

const wechat = {
		
	initJssdk: function(callback) {
		let url = location.href
		app.request('api/wechat/config', {url:url}).then(res => {
			if (res.data) {
				jweixin.config({
					debug: false,
					appId: res.data.appid,
					timestamp: res.data.timestamp,
					nonceStr: res.data.nonceStr,
					signature: res.data.signature,
					jsApiList: [
						'openLocation',
						'getLocation'
					]
				});
				//配置完成后，再执行定位等功能  
				if (callback) {
					callback(res.data);
				}
			}
		})

	},

	location: function(callback) {
		this.initJssdk(function() {
			jweixin.ready(function() {
				jweixin.getLocation({
					type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入‘gcj02‘
					success: function(res) {
						console.log('定位把宝贝')
						app.save('longitude', res.longitude)
						app.save('latitude', res.latitude)
						callback(res)
					},
					fail: function(err) {
						console.log(err)
					},

				})
			})
		})
	},
	openlocation:function(obj){
		var res = obj
		this.initJssdk(function() {
			jweixin.ready(function(callback) {
				console.log('zhunbeihaole')
			jweixin.openLocation(res);
			})
		})
	}

}

export default wechat
